package com.onenine.app.danmu;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Build;
import android.text.TextPaint;

import com.onenine.common.util.SizeUtils;

import master.flame.danmaku.danmaku.model.BaseDanMu;
import master.flame.danmaku.danmaku.model.android.SpannedCacheStuffer;

/**
 * 绘制背景(自定义弹幕样式)
 */
public class BackgroundCacheStuffer extends SpannedCacheStuffer {
    // 通过扩展SimpleTextCacheStuffer或SpannedCacheStuffer个性化你的弹幕样式
    private final Paint paint = new Paint();
    private int mBackgroundColor;
    private int mPadding, mRadius;

    public BackgroundCacheStuffer(int color, int padding, int radius) {
        mBackgroundColor = color;
        mPadding = padding;
        mRadius = radius;
    }

    @Override
    public void measure(BaseDanMu danmaku, TextPaint paint, boolean fromWorkerThread) {
        danmaku.padding = SizeUtils.dp2px(30);  // 在背景绘制模式下增加padding
        super.measure(danmaku, paint, fromWorkerThread);
    }

    @Override
    public void drawBackground(BaseDanMu danmaku, Canvas canvas, float left, float top) {
        paint.setColor(mBackgroundColor);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            canvas.drawRoundRect(left + mPadding, top + mPadding
                    , left + danmaku.paintWidth - mPadding, top + danmaku.paintHeight - mPadding, mRadius, mRadius, paint);
        } else {
            canvas.drawRect(left + mPadding, top + mPadding
                    , left + danmaku.paintWidth - mPadding, top + danmaku.paintHeight - mPadding, paint);
        }
    }

    @Override
    public void drawStroke(BaseDanMu danmaku, String lineText, Canvas canvas, float left, float top, Paint paint) {
        // 禁用描边绘制
    }

    public void setmBackgroundColor(int mBackgroundColor) {
        this.mBackgroundColor = mBackgroundColor;
    }

}
